Skip to content

feat: sparsify snapshots to reduce disk usage by ~67% on Linux and Windows - #12

Merged
danbugs merged 3 commits into
mainfrom
sparsify-snapshot-upstream
May 12, 2026
Merged

feat: sparsify snapshots to reduce disk usage by ~67% on Linux and Windows#12
danbugs merged 3 commits into
mainfrom
sparsify-snapshot-upstream

Conversation

@danbugs

@danbugs danbugs commented May 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • After writing a snapshot to disk, scan for zero-filled 4 KiB pages and punch holes so the filesystem reclaims the space
  • ~80% of pages in a pyhl snapshot are all-zeros (unused heap from the buddy allocator), so a 2 GiB apparent file drops to ~650 MiB on disk
  • Zero runtime cost — mmap of sparse files behaves identically to dense files

How it works

Linux: fallocate(PUNCH_HOLE | KEEP_SIZE) on each zero page

Windows: FSCTL_SET_SPARSE + FSCTL_SET_ZERO_DATA via DeviceIoControl, with contiguous zero pages coalesced into ranges for fewer syscalls

Other platforms: no-op fallback

Changes

  • host/src/lib.rs: sparsify_snapshot() called after save_snapshot() writes the file, with platform-specific implementations
  • host/src/bin/pyhl.rs: disk_mib() reports actual on-disk size (Linux: stat blocks, Windows: GetCompressedFileSizeW) alongside apparent size
  • host/Cargo.toml: adds libc, nix (Linux) and windows-sys (Windows) dependencies for the syscalls

danbugs added 3 commits May 12, 2026 04:49
After writing the HLS snapshot, scan for all-zero 4 KiB pages and
punch them with fallocate(PUNCH_HOLE). This makes the file sparse:
zeros still read back via mmap but consume no disk blocks.

On a typical pyhl snapshot (Python + pandas/numpy warm):
  Before: 2016 MiB on disk
  After:   648 MiB on disk (same 2016 MiB apparent size)

No load-time overhead — mmap of sparse files is identical to dense.
Linux-only; other platforms get a no-op fallback.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
… HANDLE type

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs
danbugs merged commit 67398e3 into main May 12, 2026
71 checks passed
@danbugs
danbugs deleted the sparsify-snapshot-upstream branch May 12, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant